<template>
  <div class="page index" ref="page" @scroll="pageScroll">
    <van-sticky>
      <div :class="navbgClass">
        <van-search
          slot="title"
          placeholder="输入名称搜索"
          shape="round"
          background="transparent"
          v-model="value"
          @search="onSearch"
          @cancel="onCancel"
        />
      </div>
    </van-sticky>
    <van-pull-refresh id="pullRefresh" v-model="refreshing" @refresh="onRefresh">
      <van-list
          v-model="loading"
          :finished="finished"
          :offset="1"
          finished-text="没有更多了"
          @load="onLoad"
      >
        <div v-for="item in list" :key="item.id" class="normal-task">
          <van-row gutter="5">
            <van-col span="6">
              <van-image
                  round
                  width="3rem"
                  height="3rem"
                  src="https://img01.yzcdn.cn/vant/cat.jpeg"
              />
            </van-col>
            <van-col span="12" class="content">
              <div>Eric</div>
              <div class="task">简单任务</div>
              <div class="task">已做12人，剩18人</div>
            </van-col>
            <van-col span="6" class="price">
              ￥20.22
            </van-col>
          </van-row>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script >

export default {
  name: "product",
  data() {
    return {
      value:'',
      list: [
        {id:1,url:'2dfa',image:'/static/logo.png',title:'打发撒旦'},
        {id:2,url:'2dfa',image:'/static/logo.png',title:'打发撒旦'},
        {id:3,url:'2dfa',image:'/static/logo.png',title:'打发撒旦'},
        {id:4,url:'2dfa',image:'/static/logo.png',title:'打发撒旦'},
        {id:5,url:'2dfa',image:'/static/logo.png',title:'打发撒旦'},
        {id:6,url:'2dfa',image:'/static/logo.png',title:'打发撒旦'},
        {id:7,url:'2dfa',image:'/static/logo.png',title:'打发撒旦'},
        {id:8,url:'2dfa',image:'/static/logo.png',title:'打发撒旦'},
      ],
      loading: false,
      finished: false,
      refreshing: false,
      noData:false,
      page:{},
      pageIndex:1,
      pageTotal:0,
      navbgClass: "bg-transparent"
    };
  },
  mounted() {
    // this.loadData();
  },
  methods: {
    onRefresh() {
      this.pageIndex = 1;
      // 清空列表数据
      this.finished = false;
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
    onLoad() {
      setTimeout(() => {
        if (this.refreshing) {
          this.list = [];
          this.refreshing = false;
        }
        console.log(888)
        console.log(this.value)
        this.$api.get('task/taskpage?keyword='+this.value).then(json=>{
         console.log(json)
        }).catch(err=>{
          this.$toast.fail(err.msg||'系统繁忙')
        })
        this.loading = false;

        if (this.list.length >= this.pageTotal) {
          this.finished = true;
        }
        this.loading = false;

        if (this.list.length >= this.pageTotal) {
          this.finished = true;
        }
      }, 1000);
      if(this.list.length>0){
        this.noData = false;
      }else{
        this.noData = true;
      }
    },
    pageScroll() {
      let scrollTop = this.$refs.page.scrollTop;
      if (scrollTop >= window.innerWidth * 0.4) {
        this.navbgClass = "search-box";
      } else {
        this.navbgClass = "search-box bg-transparent";
      }
    },
    loadData() {
      this.$api.get("member.credit_order/counts").then(json => {
        this.credit_counts = json.data;
      });
    },
    toUrl(url) {
      if(!url){
        this.$toast('功能即将上线~')
        return;
      }
      if(url.indexOf('http://')===0 || url.indexOf('https://')===0){
        location.href = url;
      }else{
        this.$router.push(url)
      }
    },
    onSearch() {
      this.$router.push('/search?keyword='+encodeURIComponent(this.value))
    },
    onCancel() {
      this.value = ''
    },
    sorry() {
      this.$toast('功能即将上线~')
    }
  }
};
</script>
<style lang="scss" >
.index {
  .normal-task{
    padding-top: 1rem;
    padding-bottom: 1rem;
    margin-top: 1rem;
    background: #fff;
    text-align: center;
    .content{
      text-align: left;
      .task{
        color: gray;
      }
    }
    .price{
      color: red;
      font-size: 1rem;
      font-weight: bold;
    }
  }
  .search-box {
    background: #fff;
    .van-search__content {
      background-color: #ededed;
    }
    &.bg-transparent {
      background: transparent;
    }
  }
}
</style>
